﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    public partial class Detail_emp_admin : System.Web.UI.Page
    {
        public string ConvestToDate_check(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split(' ');
                stemp = stemp[0].Split('/');
                day = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                years = Convert.ToInt32(stemp[2]);
                if (years > 2300)
                {
                    years = years - 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                if (day2.Length == 1)
                {
                    day2 = "0" + day2;
                }
                if (mount2.Length == 1)
                {
                    mount2 = "0" + mount2;
                }
                return (day2 + "-" + mount2 + "-" + years2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            if (day3.Length == 1)
            {
                day3 = "0" + day3;
            }
            if (mount3.Length == 1)
            {
                mount3 = "0" + mount3;
            }
            return (day3 + "-" + mount3 + "-" + years3);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 3)
                    {
                        int emp_id = Int32.Parse(Session["emp_id"].ToString());
                        string user_detail = @"select *
                                from Table_Personnel
                                inner join Table_positions
                                    on Table_Personnel.Positions_id = Table_positions.Positions_id
                                inner join District
                                    on Table_Personnel.Personnel_district = District.DistrictId
                                inner join Amphur
                                    on Table_Personnel.Personnel_sub_district = Amphur.AmphurId
                                inner join Province
                                    on Table_Personnel.Personnel_province = Province.ProvinceId
                                where Table_Personnel.Personnel_id = " + emp_id + "";
                        SqlCommand sqlcomm = new SqlCommand();
                        string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                        SqlConnection sqlconn = new SqlConnection(Connection);
                        SqlDataAdapter da = new SqlDataAdapter(user_detail, sqlconn);
                        DataTable dt = new DataTable();
                        da.Fill(dt);

                        try
                        {
                            TextBox18.Text = dt.Rows[0]["Personnel_id"].ToString();
                            TextBox13.Text = dt.Rows[0]["Personnel_prefix"].ToString();
                            TextBox1.Text = dt.Rows[0]["Personnel_name"].ToString();
                            TextBox2.Text = dt.Rows[0]["Personnel_surname"].ToString();
                            TextBox14.Text = dt.Rows[0]["Personnel_sex"].ToString();
                            TextBox4.Text = ConvestToDate_check(dt.Rows[0]["Personnel_birthday"].ToString());
                            TextBox5.Text = dt.Rows[0]["Personnel_home_no"].ToString();
                            TextBox6.Text = dt.Rows[0]["Personnel_village_no"].ToString();
                            TextBox7.Text = dt.Rows[0]["Personnel_lane"].ToString();
                            TextBox12.Text = dt.Rows[0]["Personnel_road"].ToString();
                            TextBox17.Text = dt.Rows[0]["DistrictName"].ToString();
                            TextBox16.Text = dt.Rows[0]["AmphurName"].ToString();
                            TextBox15.Text = dt.Rows[0]["ProvinceName"].ToString();
                            TextBox8.Text = dt.Rows[0]["Personnel_post"].ToString();
                            TextBox9.Text = dt.Rows[0]["Personnel_mail"].ToString();
                            TextBox11.Text = dt.Rows[0]["Personnel_tel"].ToString();
                            TextBox3.Text = dt.Rows[0]["Personnel_identification"].ToString();
                            TextBox19.Text = dt.Rows[0]["Positions_name"].ToString(); ;

                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }                         
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["id_emp"] = TextBox18.Text;
            Response.Redirect("change_Detail_emp.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("Search_emp.aspx");
        }
    }
}